<template>
    <div class="inspection-container">
        <div class="action-buttons">
            <el-button type="primary" size="small" plain circle @click="emit('showUpload', imageCategory)">
                <el-icon>
                    <Plus />
                </el-icon>
            </el-button>
        </div>

        <div class="inspection-images">
            <div v-if="images.length === 0" class="no-images">
                <el-empty description="暂无检验图片" />
            </div>
            <div v-else class="image-grid">
                <div v-for="(image, index) in images" :key="index" class="image-item">
                    <el-image :src="image.url" :preview-src-list="imageUrls" fit="cover" class="inspection-image" />
                    <div class="image-actions">
                        <el-button type="danger" size="small" circle @click="emit('deleteImage', image)">
                            <el-icon>
                                <Delete />
                            </el-icon>
                        </el-button>
                    </div>
                </div>
                <div class="add-image-item">
                    <el-button type="primary" plain class="add-button" @click="emit('showUpload', imageCategory)">
                        <el-icon>
                            <Plus />
                        </el-icon>
                    </el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'

interface ImageItem {
    url: string
    uid: string
}

const props = defineProps<{
    images: ImageItem[]
    imageCategory?: string
}>()

// 设置默认值
const imageCategory = props.imageCategory || '检验报告'

const emit = defineEmits<{
    (e: 'showUpload', imageCategory: string): void
    (e: 'deleteImage', image: ImageItem): void
}>()

const imageUrls = computed(() => props.images.map(img => img.url))
</script>

<style scoped>
.inspection-container {
    position: relative;
    width: 100%;
}

.action-buttons {
    position: absolute;
    top: -40px;
    right: 0;
    display: flex;
    gap: 8px;
}

.inspection-images {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    width: 100%;
}

.no-images {
    width: 100%;
    padding: 20px;
    background-color: #f5f7fa;
    border-radius: 4px;
    border: 1px dashed #dcdfe6;
    display: flex;
    justify-content: center;
    align-items: center;
}

.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 15px;
    width: 100%;
}

.image-item {
    position: relative;
    border: 1px solid #eee;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f7fa;
    height: 120px;
}

.inspection-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.image-actions {
    position: absolute;
    top: 5px;
    right: 5px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    padding: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
}

.image-actions .el-button {
    padding: 0;
    margin: 0;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}

.add-image-item {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px dashed #dcdfe6;
    border-radius: 4px;
    background-color: #f5f7fa;
    height: 120px;
}

.add-button {
    padding: 0;
    margin: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}
</style>